<?php

function get_goods_sku_arr($goods_id)
{

    $goods_sku_arr = c_getRows("select * from tbl_goods_sku  WHERE goods_id = " . $goods_id);
    if ($goods_sku_arr) {
        $sku_info_key = array_column($goods_sku_arr, 'sku_info_key');
        $sku_info_name = array_column($goods_sku_arr, 'sku_info_name');
        $sku_info_name_res = [];
        foreach ($sku_info_name as $sn) {
            $sn_arr = explode(',', $sn);
            $sku_info_name_res = array_merge($sku_info_name_res, $sn_arr);
        }
        $sku_info_name_arr = array_values(array_unique($sku_info_name_res));
        $sku_info_name_arr_sql = implode(',', $sku_info_name_arr);
        $sku_info_name_total_arr = c_getRows("select id,sku_value from tbl_sku_value WHERE id IN ($sku_info_name_arr_sql)");
        $sku_info_name_total_arr = array_column($sku_info_name_total_arr, 'sku_value', 'id'); // 所有value

        $sku_info_key_ch_arr = c_getRows("select id,sku_name from tbl_sku_key WHERE id IN (" . $sku_info_key[0] . ")");
        $sku_info_key_ch_arr = array_column($sku_info_key_ch_arr, 'sku_name', 'id'); // 所有key集合
        foreach ($goods_sku_arr as &$goods_sku) {
            $goods_sku['sku_info_key_ch_arr'] = $sku_info_key_ch_arr;
            $sku_info_name_list = explode(',', $goods_sku['sku_info_name']);
            $rr = [];
            foreach ($sku_info_name_list as $sl) {
                $rr[$sl] = $sku_info_name_total_arr[$sl];
            }
            $goods_sku['sku_info_name_ch_arr'] = $rr;

        }

    } else {
        $goods_sku_arr = '';
    }

    return $goods_sku_arr;
}

function get_goods_sku_show_list($goods_id)
{

    $goods_sku_arr = c_getRows("select * from tbl_goods_sku  WHERE goods_id = " . $goods_id);
    $res_show_arr = [];
    if ($goods_sku_arr) {
        $sku_info_key = array_column($goods_sku_arr, 'sku_info_key');
        $sku_info_name = array_column($goods_sku_arr, 'sku_info_name');
        $sku_info_name_res = [];
        foreach ($sku_info_name as $sn) {
            $sn_arr = explode(',', $sn);
            $sku_info_name_res = array_merge($sku_info_name_res, $sn_arr);
        }
        $sku_info_name_arr = array_values(array_unique($sku_info_name_res));
        $sku_info_name_arr_sql = implode(',', $sku_info_name_arr);
        $sku_info_name_total_arr = c_getRows("select id,sku_value from tbl_sku_value WHERE id IN ($sku_info_name_arr_sql)");
        $sku_info_name_total_arr = array_column($sku_info_name_total_arr, 'sku_value', 'id'); // 所有value

        $sku_info_key_ch_arr = c_getRows("select id,sku_name from tbl_sku_key WHERE id IN (" . $sku_info_key[0] . ")");
        $sku_info_key_ch_arr = array_column($sku_info_key_ch_arr, 'sku_name', 'id'); // 所有key集合

        $i = 0;
        foreach ($sku_info_key_ch_arr as $id => $sik) {
            foreach ($goods_sku_arr as $goods_sku) {
                $sku_info_name_list = explode(',', $goods_sku['sku_info_name']);
                if (!in_array($sku_info_name_list[$i], $res_show_arr[$id])) {
                    $res_show_arr[$id][] = $sku_info_name_list[$i];
                }

            }
            $i++;
        }


    }

    foreach ($res_show_arr as &$ss) {
        foreach ($ss as $x => $id) {
            $ss[$x] = ['id' => $id, 'val' => $sku_info_name_total_arr[$id]];
        }
    }
    return [$res_show_arr, $sku_info_key_ch_arr];

}

function get_sku_info_by_id($goods_sku_id)
{
    $goods_sku_arr = c_getRow("select * from tbl_goods_sku  WHERE id = " . $goods_sku_id . " limit 1");
    $sku_price = $goods_sku_arr['price'];
    $sku_store = $goods_sku_arr['sku_store'];
    $sku_info_key_list = $goods_sku_arr['sku_info_key'];
    $sku_info_key_arr = explode(',', $sku_info_key_list);
    $sku_info_name_list = $goods_sku_arr['sku_info_name'];
    $sku_info_name_arr = explode(',', $sku_info_name_list);
    $sku_info_key_values = c_getRows("select id as key_id,sku_name as sku_key_name from tbl_sku_key WHERE id in ($sku_info_key_list) ");
    $sku_info_name_values = c_getRows("select id as value_id,sku_value as sku_value_name from tbl_sku_value WHERE id in ($sku_info_name_list) ");

    $res = '';
    foreach ($sku_info_key_values as $k => $v){
        $res[] = array_merge($v,$sku_info_name_values[$k]);
    }
    return [$sku_info_key_arr,$sku_info_name_arr,$sku_price,$sku_store,$res];
}

?>